﻿$(function () {
    $("#prev").mouseenter(function () {
        $("#pic_list").show();
        $("#transparence").show();
        $("#prev img").css("margin-top", "-92px");
    });
    $("#prev").mouseleave(function () {
        $("#pic_list").hide();
        $("#transparence").hide();
        $("#prev img").css("margin-top", "0");
    });
    $("#next").mouseenter(function () {
        $("#pic_list").show();
        $("#transparence").show();
        $("#next img").css("margin-top", "-92px");
    });
    $("#next").mouseleave(function () {
        $("#pic_list").hide();
        $("#transparence").hide();
        $("#next img").css("margin-top", "0");
    });

    //单击切换到上一个、下一个
    $("#prev").click(function () {
        var index = 0;
        $("#pic_list img").each(function () {
            if ($(this).attr("src") == $("#this_pic").attr("src")) {
                index = $(this).parent().parent().index();
                return;
            }
        });
        if (index == 0) {
            index = $("#pic_list img").length - 1;
            $("#pic_list img").eq(index).mouseover();
            return;
        }
        index--;
        $("#pic_list img").eq(index).mouseover();
    });

    $("#next").click(function () {
        var index = 0;
        $("#pic_list img").each(function () {
            if ($(this).attr("src") == $("#this_pic").attr("src")) {
                index = $(this).parent().parent().index();
                return;
            }
        });
        if (index == $("#pic_list img").length - 1) {
            index = 0;
            $("#pic_list img").eq(index).mouseover();
            return;
        }
        index++;
        $("#pic_list img").eq(index).mouseover();
    });

    $("#pic_box a:eq(0)").width(190).height(190);

    $("#pic_box a img").load(function () {
        var width = 0;
        var height = 0;
        width = $(this).width();
        height = $(this).height();
        var $firstImg = $("#pic_box a img").eq(0);
        if ($(this).attr("src") == $firstImg.attr("src")) {
            if (width > height) {
                $(this).width(width / height * 190);
                $(this).height(190);
                width = width / height * 190;
                var margin = 95 - width / 2;
                $(this).css("margin-left", margin).css("margin-right", margin);
            }
            else {
                $(this).height(height / width * 190);
                $(this).width(190);
                height = height / width * 190;
                var margin = 95 - height / 2;
                $(this).css("margin-top", margin).css("margin-bottom", margin);
            }
        }
        else {
            if (width > height) {
                $(this).width(width / height * 90);
                $(this).height(90);
                width = width / height * 90;
                var margin = 45 - width / 2;
                $(this).css("margin-left", margin).css("margin-right", margin);
            }
            else {
                $(this).height(height / width * 90);
                $(this).width(90);
                height = height / width * 90;
                var margin = 45 - height / 2;
                $(this).css("margin-top", margin).css("margin-bottom", margin);
            }
        }
    });

    $("#pic_box a").hover(function () {
        var pid = $(this).children(0).attr("alt");
        var parm = { 'pid': pid };
        $.ajax({
            url: "AJAX/GetPhotoInfo.ashx?op=getInfo",
            type: 'POST',
            data: parm,
            dataType: "json",
            success: function (json) {
                $("#lblAuthor").text(json.userName);
                $("#lblLocation").text(json.position);
                $("#lblUploadTime").text(json.uploadTime);
                $("#lblType").text(json.tagName);
                $("#lblPhotoDescrip").text(json.description);
                $("#imagePreview").attr("src", json.src);
            }
        });
    });

    $("#imagePreview").load(function () {
        if (this.naturalWidth) {
            //如果支持HTML5的naturalWidth和naturalHeight属性
            var width = this.naturalWidth;
            var height = this.naturalHeight;
            height = height / width * 350;
            $(this).width(350);
            $(this).height(height);
        }
        else {
            //如果不支持HTML5的naturalWidth和naturalHeight属性
            var image = new Image();
            image.src = this.src;
            var width = image.width;
            var height = image.height;
            height = height / width * 350;
            $(this).width(350);
            $(this).height(height);
        }
    });

    $(".news dt a img").load(function () {
        var height = $(this).height();
        var width = $(this).width();
        if (width > height) {
            $(this).width(width / height * 120);
            $(this).height(120);
            width = width / height * 120;
            var margin = 60 - width / 2;
            $(this).css("margin-left", margin).css("margin-right", margin);
        }
        else {
            $(this).height(height / width * 120);
            $(this).width(120);
            height = height / width * 120;
            var margin = 60 - height / 2;
            $(this).css("margin-top", margin).css("margin-bottom", margin);
        }
    });
});